package com.ruoyi.web.controller.cpsp;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.cpsp.domain.RegulationDetail;
import com.ruoyi.cpsp.service.RegulationDetailService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author neu.xiaojiacheng
 * @version 1.0
 * @date 2022/2/27 20:28
 */

@CrossOrigin
@RestController
@RequestMapping("/cpsp/regulationdetail")
public class RegulationDetailController {

    @Autowired
    private RegulationDetailService regulationDetailService;

    @ApiOperation("添加记录")
    @RequestMapping(value = "/addForm", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult addForm(@RequestParam Integer regulationTypeId,
                              @RequestParam String code,
                              @RequestParam String describe){
        RegulationDetail record = new RegulationDetail();
        record.setRegulationTypeId(regulationTypeId);
        record.setCode(code);
        record.setDescribe(describe);

        if (regulationDetailService.submitAllInfo(record))
            return AjaxResult.success();
        else
            return AjaxResult.error("保存失败");
    }

    /*返回给前端需要的规则种类*/
    //@PreAuthorize(hasPermi = "regulationtype:select")
    @ApiOperation("查找")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult select(@RequestParam Integer pageSize,
                         @RequestParam Integer currentPage,
                         @RequestParam Integer typeid) {
        if (typeid == null)
            return null;
        else
            return AjaxResult.success(regulationDetailService.getByTypeId(pageSize, currentPage, typeid));
    }

    /*返回给前端需要的规则种类*/
    //@PreAuthorize(hasPermi = "regulationtype:select")
    @ApiOperation("根据typeID以及描述查找")
    @RequestMapping(value = "/selectAll",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult selectAll(@RequestParam Integer pageSize,
                         @RequestParam Integer currentPage,
                         @RequestParam Integer typeid,
                         @RequestParam String describe) {
        if (typeid == null && describe==null)
            return null;
        else
            return AjaxResult.success(regulationDetailService.getByTypeIdAndDescribe(pageSize,currentPage,typeid,describe));
    }

    //@PreAuthorize("@ss.hasPermi('system:detail:edit')")
    @ApiOperation("删除")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return AjaxResult.success(regulationDetailService.delete(id));
    }

    @ApiOperation("修改")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult update(@RequestParam Integer id,
                         @RequestParam Integer regulationTypeId,
                         @RequestParam String code,
                         @RequestParam String describe) {

        RegulationDetail record = new RegulationDetail();
        record.setId(id);
        record.setRegulationTypeId(regulationTypeId);
        record.setCode(code);
        record.setDescribe(describe);
        return AjaxResult.success(regulationDetailService.update(record));
    }
}
